package PHANVANVUONG.SERLALIZABLE.DEMO;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.filechooser.FileNameExtensionFilter;

@SuppressWarnings("serial")
public class ViewsReadFile extends JFrame{
	private JTable table;
	private final static String[] header=new String[]{"Mã sinh viên", "Họ và tên", "Điểm số"};
	private static List<StudentEntity> lisObj;
	
	public ViewsReadFile(String tittle, String[] header, List<Object[]> lisValu) {
		// TODO Auto-generated constructor stub
		super(tittle);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		TableModel model=new TableModel(header, lisValu);
		table=new JTable(model);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		JScrollPane scroll=new JScrollPane(table);
		add(scroll, BorderLayout.CENTER);
		setSize(400, 200);
		setLocation(500, 200);
	}
	
	public static void main(String[] args) {
		JFileChooser objFile = new JFileChooser();
        objFile.setCurrentDirectory(new java.io.File("."));
        objFile.setDialogTitle("Chọn file: ");
        objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        String[] EXTENSION = new String[] { "dat" };
        FileNameExtensionFilter filter = new FileNameExtensionFilter("DAT files", EXTENSION);
        objFile.setFileFilter(filter);
        objFile.setMultiSelectionEnabled(false);

        int returnVal = objFile.showOpenDialog(null);
        String ParentPath = "";
        String FileName = "";
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            ParentPath = objFile.getSelectedFile().getParent();
            FileName = objFile.getSelectedFile().getName();
            Controllers<StudentEntity> objC=new Controllers<StudentEntity>();
            List<Object[]> lisValu=new ArrayList<Object[]>();
            Object[] objValu;
            StudentEntity stu=null;
            try {
    			lisObj=objC.readFile(ParentPath, FileName);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
            
            for(int i=0; i<lisObj.size(); i++){
            	stu=lisObj.get(i);
            	objValu=new Object[]{stu.getRollNo(), stu.getFullName(), stu.getMark()};
            	lisValu.add(objValu);
            }
            
            ViewsReadFile table=new ViewsReadFile("Thông tin sinh viên", header, lisValu);
            table.setVisible(true);
        }
        
        
	}
}
